# 多阶段构建：前端 + Rust
FROM node:18-bullseye AS frontend

# 安装 Bun
RUN npm install -g bun

WORKDIR /app

# 复制前端文件
COPY package.json ./
COPY bun.lock ./
COPY vite.config.ts ./
COPY tsconfig.json ./
COPY tailwind.config.js ./
COPY postcss.config.js ./
COPY index.html ./
COPY src/ ./src/
COPY public/ ./public/

# 安装前端依赖
RUN bun install --frozen-lockfile

# 构建前端
RUN bun run build

# Rust 构建阶段
FROM rust:1.70-bullseye AS backend

WORKDIR /app

# 复制 Rust 项目文件
COPY src-tauri/ ./src-tauri/

# 安装依赖并构建
RUN cd src-tauri && \
    cargo build --release

# 最终运行阶段
FROM debian:bullseye-slim

# 安装必要依赖
RUN apt-get update && \
    apt-get install -y \
    libwebkit2gtk-4.0-dev \
    build-essential \
    curl \
    wget \
    libssl-dev \
    libgtk-3-dev \
    libayatana-appindicator3-dev \
    librsvg2-dev && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

WORKDIR /app

# 从构建阶段复制文件
COPY --from=frontend /app/dist/ ./dist/
COPY --from=backend /app/src-tauri/target/release/custom_google ./app
COPY --from=backend /app/src-tauri/target/release/ ./src-tauri/target/release/

# 设置执行权限
RUN chmod +x ./app

# 暴露端口（如果需要）
EXPOSE 1420

# 启动应用
CMD ["./app"]